package com.qububu.share.reply.control;

import static com.qububu.utils.ParamConverter.assertNotEmpty;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qububu.common.user.dto.UserInfoDto;
import com.qububu.share.reply.dto.MessageInfoDto;
import com.qububu.share.reply.service.ReplyService;
import com.qububu.utils.DateUtil;
import com.qububu.utils.SystemConstants;
import com.qububu.utils.SystemUtil;

/**
 * @功能  评论的相关处理
 * @详情 
 * */
@Controller
@RequestMapping("/articleMessage")
public class ReplyControl {

	@Resource
	private ReplyService replyService;
	
	/**
	 * @功能 添加文章的评论
	 * @author LH
	 * */
	@RequestMapping("/addMessage")
	public String addMessage(HttpServletRequest request, HttpServletResponse response) {
		MessageInfoDto messageInfo = gainMessageInfoDto(request, response);
		
		replyService.doAddMessage(messageInfo);
		
		return "index"; 
	}
	
	/**
	 * @功能 遍历文章所对应的评论
	 * @author LH
	 * @return 评论列表
	 * */
	@RequestMapping(value="/ajaxQueryMessage")
	@ResponseBody
	public Map<String, Object> ajaxQueryMessage(HttpServletRequest request, HttpServletResponse response) {
		String article_id = assertNotEmpty(request.getParameter("article_id"));
		Map<String, Object> messageResultMap = null;
		
		ArrayList<MessageInfoDto> messageList = replyService.getQueryMessage(article_id);
		messageResultMap = new HashMap<String, Object>();
		messageResultMap.put("result", SystemConstants.AJAX_SUCCESS);
		messageResultMap.put("messageList", messageList);
		
		return messageResultMap;
	} 
	
	/**
	 * @功能 删除对文章的评论
	 * @author LH
	 */
	@RequestMapping(value="/ajaxDeleteMessageForArticle")
	@ResponseBody
	public boolean ajaxDeleteMessageForArticle(HttpServletRequest request, HttpServletResponse response){
		String parentMsgId = assertNotEmpty(request.getParameter("parent_msg_id"));
		String msgId = assertNotEmpty(request.getParameter("msg_id"));
		
		replyService.doDeleteMessage(parentMsgId, msgId);
		
		return true;
	}
	
	/**
	 * @功能 得到评论内容
	 * @author LH
	 * @return MessageInfoDto 评论内容
	 */
	private MessageInfoDto gainMessageInfoDto(HttpServletRequest request, HttpServletResponse response){
		String articleId = assertNotEmpty(request.getParameter("article_id"));
		String parentMsgId = assertNotEmpty(request.getParameter("parent_msg_id"));
		String msgContext = assertNotEmpty(request.getParameter("msg_content"));
		String msgCreateName = assertNotEmpty(request.getParameter("msg_create_name"));
		
		UserInfoDto dto = (UserInfoDto) request.getSession().getAttribute(SystemConstants.LOGINUSER_SESSION_NAME);
		String userId = String.valueOf(dto.getUserId());
		String msgCreateIp = assertNotEmpty(SystemUtil.getRequestIp(request));
		msgContext = SystemUtil.convertSpecialMark(msgContext);
		String msgSummaryPicUrl = SystemUtil.getSummaryPicUrl(msgContext, request.getContextPath());
		int msgCreateDttm = DateUtil.convertCurrentDTTMtoInt();
		String msgId = SystemUtil.getUUIDString();
		
		return createMessageInfoDto(msgId, msgContext, msgCreateName, msgSummaryPicUrl, msgCreateDttm, msgCreateIp, parentMsgId, articleId, userId);
	}
	
	/**
	 * @功能 设置评论内容
	 * @author LH
	 * @return MessageInfoDto 设置过后的评论内容
	 */
	private MessageInfoDto createMessageInfoDto(String msgId, String msgConText, 
			String msgCreateName, String msgSummaryPicUrl, 
			int msgCreateDttm, String msgCreateIp, String parentMsgId, 
			String articleId, String userId){
		MessageInfoDto messageInfo = new MessageInfoDto(msgId, userId, parentMsgId, articleId);
		
		messageInfo.setMsgText(msgConText);
		messageInfo.setMsgCreateName(msgCreateName);
		messageInfo.setMsgSummaryPicUrl(msgSummaryPicUrl);
		messageInfo.setMsgCreateDttm(msgCreateDttm);
		messageInfo.setMsgCreateIp(msgCreateIp);
		
		return messageInfo;
	}
}
